package cn.com.gridinfo.sooc.utils;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.widget.Button;
import android.widget.DatePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {
	private Activity context;

	public DateUtil(Activity context) {
		super();
		this.context = context;
	}

	/*
	 * 选择时间
	 */
	public void selectTime(final Button button) {
		final Calendar calendar = Calendar.getInstance();
		Dialog dialog = null;

		DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker datePicker, int year, int month,
					int dayOfMonth) {

				datePicker.setMaxDate(calendar.getTime().getTime()
						+ calendar.getTime().getTime());
				// Calendar月份是从0开始,所以month要加1
				String MM = null,DD = null;
				month = month+1;
				if(month<10){					
					MM = "0"+month+"";
				}else{
					MM = +month+"";
				}
				if(dayOfMonth<10){
					DD = "0"+dayOfMonth+"";
				}else{
					DD = dayOfMonth+"";
				}
				String showTime = year + "-" + MM + "-" + DD;
				button.setText(showTime);
			}
		};
		dialog = new DatePickerDialog(context, dateListener,
				calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
		dialog.show();
	}

	/*
	 * 日期转换为时间戳工具
	 */
	public Long getStartTimestamp(String date) throws Exception {

		Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date
				+ " 00:00:00");
		Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
				.parse("1970/01/01 8:00:00");
		Long ll = date1.getTime();
		Long mm = date2.getTime();
		Long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime()
				- date2.getTime() : date2.getTime() - date1.getTime();
		return l / 1000;
	}

	/*
	 * 日期转换为时间戳工具
	 */
	public Long getEndTimestamp(String date) throws Exception {

		Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date
				+ " 00:00:00");
		Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
				.parse("1970/01/01 8:00:00");
		Long ll = date1.getTime();
		Long mm = date2.getTime();
		Long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime()
				- date2.getTime() : date2.getTime() - date1.getTime();
		return l / 1000;
	}
	public static String getDateFromDatestamp(String dateStampString) {
		Long time = Long.parseLong(dateStampString)*1000;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		sf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String dstr = sf.format(time);		
		String sd = sf.format(new Date(time)); 
		return sd;
	}
	//常用
	public static String datestamp2Date(String dateStampString) {
		Long time = Long.parseLong(dateStampString)*1000;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		sf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String dstr = sf.format(time);
		String sd = sf.format(new Date(time)); 
		return sd;
	}
	/**
	 * 不显示年
	 * @param dateStampString
	 * @return
	 */
	public static String datestampToDate(String dateStampString) {
		Long time = Long.parseLong(dateStampString)*1000;
		SimpleDateFormat sf = new SimpleDateFormat("MM-dd HH:mm");
		sf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String dstr = sf.format(time);
		String sd = sf.format(new Date(time)); 
		return sd;
	}


	/**
	 * 获取当前时间戳
	 */
	public static long getCurrDateStamp(){
		return new Date().getTime();
	}
}
